package com.chuyh.libraries.test

import android.graphics.Color
import android.os.Bundle
import android.view.Gravity
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.TextView
import androidx.core.view.setMargins
import androidx.core.view.setPadding
import androidx.core.widget.NestedScrollView
import androidx.fragment.app.FragmentActivity
import androidx.lifecycle.lifecycleScope
import com.noober.background.drawable.DrawableCreator
import kotlinx.coroutines.launch

/**
 * Date:    2023/11/1
 * Author:  chuyh
 * Des:     基础测试页面
 */
open class BaseTestActivity : FragmentActivity() {

    private lateinit var rootContainer: LinearLayout

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(NestedScrollView(this).apply {
            layoutParams = ViewGroup.LayoutParams(-1, -1)
            addView(LinearLayout(context).apply {
                rootContainer = this
                layoutParams = ViewGroup.LayoutParams(-1, -2)
                orientation = LinearLayout.VERTICAL
            })
        })
        initViews()
    }

    open fun initViews() {}

    protected fun addClickView(label: String, onClick: suspend (View) -> Unit): TextView {
        return rootContainer.addClickView(label, onClick)
    }

    private fun ViewGroup.addClickView(label: String, onClick: (suspend (View) -> Unit)? = null): TextView {
        return TextView(context).apply {
            layoutParams = LinearLayout.LayoutParams(-1, -2).apply { setMargins(15.dp) }
            setPadding(15.dp)
            gravity = Gravity.CENTER
            text = label
            onClick?.let { setOnClickListener { lifecycleScope.launch { onClick.invoke(it) } } }
            addView(this)

            background = DrawableCreator.Builder().setCornersRadius(12.dpf).setStrokeWidth(1.dpf)
                .setStrokeColor(Color.parseColor("#66666666")).build()
        }
    }
}